﻿'Decorator pattern -- Structural example
Namespace DoFactory.GangOfFour.Decorator.Structural
    ''' <summary>
    ''' MainApp startup class for Structural
    ''' Decorator Design Pattern.
    ''' </summary>
    Public Class MainApp
        ''' <summary>
        ''' Entry point into console application.
        ''' </summary>
        Public Shared Sub Main()
            ' Create ConcreteComponent and two Decorators
            Dim c As New ConcreteComponent()
            Dim d1 As New ConcreteDecoratorA()
            Dim d2 As New ConcreteDecoratorB()

            ' Link decorators
            d1.SetComponent(c)
            d2.SetComponent(d1)
            d2.Operation()

        End Sub

    End Class

    ''' <summary>
    ''' The 'Component' abstract class
    ''' </summary>
    MustInherit Class Component

        Public MustOverride Sub Operation()

    End Class

    ''' <summary>
    ''' The 'ConcreteComponent' class
    ''' </summary>
    Class ConcreteComponent
        Inherits Component

        Public Overrides Sub Operation()
            Console.WriteLine("ConcreteComponent.Operation()")
        End Sub

    End Class

    ''' <summary>
    ''' The 'Decorator' abstract class
    ''' </summary>
    MustInherit Class Decorator
        Inherits Component

        Protected component As Component
        Public Sub SetComponent(ByVal component As Component)
            Me.component = component
        End Sub

        Public Overrides Sub Operation()
            If component IsNot Nothing Then
                component.Operation()
            End If
        End Sub

    End Class

    ''' <summary>
    ''' The 'ConcreteDecoratorA' class
    ''' </summary>
    Class ConcreteDecoratorA
        Inherits Decorator

        Public Overrides Sub Operation()
            MyBase.Operation()
            Console.WriteLine("ConcreteDecoratorA.Operation()")
        End Sub

    End Class

    ''' <summary>
    ''' The 'ConcreteDecoratorB' class
    ''' </summary>
    Class ConcreteDecoratorB
        Inherits Decorator

        Public Overrides Sub Operation()
            MyBase.Operation()
            AddedBehavior()
            Console.WriteLine("ConcreteDecoratorB.Operation()")
        End Sub

        Private Sub AddedBehavior()

        End Sub

    End Class

End Namespace
